package com.example.zz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.os.AsyncTask;
import android.util.Log;

public class czytajStrone {
	 static void a(String[] a){
	        for (String string : a) {
	            System.out.print(" "+string);
	        }
	            System.out.println("" );
	    }
	 boolean a(){
			        return true;
	 }
	 class RequestTask extends AsyncTask<String, String, String>{

		 @Override
		 // username, password, message, mobile
		 protected String doInBackground(String... url) {
		     // constants
		     int timeoutSocket = 5000;
		     int timeoutConnection = 5000;

		     HttpParams httpParameters = new BasicHttpParams();
		     HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
		     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
		     HttpClient client = new DefaultHttpClient(httpParameters);

		     HttpGet httpget = new HttpGet(url[0]);

		     try {
		         HttpResponse getResponse = client.execute(httpget);
		         final int statusCode = getResponse.getStatusLine().getStatusCode();

		         if(statusCode != HttpStatus.SC_OK) {
		             Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
		             return null;
		         }

		         String line = "";
		         StringBuilder total = new StringBuilder();

		         HttpEntity getResponseEntity = getResponse.getEntity();

		         BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));  

		         while((line = reader.readLine()) != null) {
		             total.append(line);
		         }

		         line = total.toString();
		         return line;
		     } catch (Exception e) {
		         Log.w("MyApp", "Download Exception : " + e.toString());
		     }
		     return null;
		 }

		 @Override
		 protected void onPostExecute(String result) {
		     // do something with result
		 }
		 }
	 void f(){
		 //String a=new RequestTask().execute("http://www.lotto.com/");
		// if(a())
	//	 {
		/*  URL textUrl;
		  try {
		   textUrl = new URL("http://www.lotto.pl");
		   BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
		   String StringBuffer;
		         String stringText = "";
		   while ((StringBuffer = bufferReader.readLine()) != null) {
		    stringText += StringBuffer;
		   }
		         bufferReader.close();
		  } catch (MalformedURLException e) {
		   e.printStackTrace();
		  } catch (IOException e) {
		   e.printStackTrace();
		  }*/
		  //}
	 }
	    public czytajStrone() throws MalformedURLException, IOException
	    {
	    	  URL oracle = new URL("http://www.lotto.com/");
	          BufferedReader in = new BufferedReader(
	          new InputStreamReader(oracle.openStream()));

	          String inputLine;
	          while ((inputLine = in.readLine()) != null)
	              System.out.println(inputLine);
	          in.close();
	    	//f();
	    	/*HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
	    	HttpGet httpget = new HttpGet("http://lotto.pl/"); // Set the action you want to do
	    	HttpResponse response = httpclient.execute(httpget); // Executeit
	    	HttpEntity entity = response.getEntity(); 
	    	InputStream is = entity.getContent(); // Create an InputStream with the response
	    	BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
	    	StringBuilder sb = new StringBuilder();
	    	String line = null;
	    	while ((line = reader.readLine()) != null) // Read line by line
	    	    sb.append(line + "\n");

	    	String resString = sb.toString(); // Result is here

	    	is.close(); */
	    	// Close the stream
	       /* HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://www.lotto.pl");
            // Get the response
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response_str = client.execute(request, responseHandler);*/
            //textView.setText(response_str);
	       /* URL yahoo = new URL("http://www.lotto.pl/");
	        URLConnection yc = yahoo.openConnection();
	        BufferedReader in = new BufferedReader(
	                                new InputStreamReader(
	                                yc.getInputStream()));
	        String inputLine;
	        boolean lotto=false,wyniklotto=false,lottoplus=false,minilotto=false,mm=false;
	        int licznik=0;
	        String[] liczbylotto={"","","","","",""},liczbylotto2={"","","","","",""},liczbylotto3={"","","","",""},
	                liczbylotto4={"","","","","","","","","","","","","","","","","","","",""};
	        while (( inputLine = in.readLine()) != null ) 
	        {
	            
	            if( inputLine.indexOf("Multi Multi")!=-1 ){
	                lotto=wyniklotto=lottoplus=minilotto=false;mm=true;
	            }
	            
	            if( inputLine.indexOf("Lotto")!=-1 ){
	                mm=wyniklotto=lottoplus=minilotto=false;lotto=true;
	            }
	            if( inputLine.indexOf("Lotto Plus")!=-1 ){
	                lotto=wyniklotto=mm=minilotto=false;lottoplus=true;
	            }
	            if( inputLine.indexOf("Mini Lotto")!=-1 ){
	                lotto=wyniklotto=mm=lottoplus=false;minilotto=true;
	            }
	            if( mm )
	            {
	                d(inputLine,">>>>>lotto mm");
	                if( inputLine.indexOf("wynik_multi-multi")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_multi-multi")+19,inputLine.indexOf("wynik_multi-multi")+21);  
	                     System.out.println("mm "+a);
	                      liczbylotto4[licznik]=a;
	                      ++licznik;
	                      if( licznik==20 )
	                      {
	                          mm=false;
	                          licznik=0;
	                      }
	                    }
	            }
	            if( minilotto )
	            {
	                d(inputLine,">>>>>lotto minilotto");
	                if( inputLine.indexOf("wynik_mini-lotto")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_mini-lotto")+18,inputLine.indexOf("wynik_mini-lotto")+20);  
	                     System.out.println("minilotto "+a);
	                      liczbylotto3[licznik]=a;
	                      ++licznik;
	                      if( licznik==5 )
	                      {
	                          minilotto=false;
	                          licznik=0;
	                      }
	                    }
	            }
	            if( lottoplus )
	            {
	                d(inputLine,">>>>>lottoplus");
	                if( inputLine.indexOf("wynik_lotto")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_lotto")+13,inputLine.indexOf("wynik_lotto")+15);  
	                     System.out.println("lottoplus "+a);
	                      liczbylotto2[licznik]=a;
	                      ++licznik;
	                      if( licznik==6 )
	                      {
	                          lottoplus=false;
	                          licznik=0;
	                      }
	                    }
	            }
	            if( lotto ) 
	            {
	                d(inputLine,">>>>>lotto");
	                    if( inputLine.indexOf("wynik_lotto")!=-1 )
	                    {
	                      String a=inputLine.substring(inputLine.indexOf("wynik_lotto")+13,inputLine.indexOf("wynik_lotto")+15);  
	                     System.out.println("lotto "+a);
	                      liczbylotto[licznik]=a;
	                      ++licznik;
	                      if( licznik==6 )
	                      {
	                          lotto=false;
	                          licznik=0;
	                      }
	                    }
	        
	            }
	        }
	        in.close();
	        a(liczbylotto);
	        a(liczbylotto2);
	        a(liczbylotto3);
	                a(liczbylotto4);*/
	    }
	    static void d(String a,String inputLine){
	                if( inputLine.indexOf("wyniki z dnia")!=-1 ) 
	                {
	                    String aa=inputLine.substring(inputLine.indexOf("wyniki z dnia")+22,inputLine.indexOf("wyniki z dnia")+30);
	                    System.out.println(a+aa);
	                }
	    }
}
